package com.bootdo.staff.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.domain.Tree;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.staff.domain.StaffMenuDO;
import com.bootdo.staff.service.StaffMenuService;
import com.bootdo.system.domain.MenuDO;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 菜单表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-04-23 13:58:59
 */
 
@Controller
@RequestMapping("/staff/staffMenu")
public class StaffMenuController {
	@Autowired
	private StaffMenuService staffMenuService;
	
	@GetMapping()
	@RequiresPermissions("staff:staffMenu:staffMenu")
	String StaffMenu(){
	    return "staff/staffMenu/staffMenu";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("staff:staffMenu:staffMenu")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<StaffMenuDO> staffMenuList = staffMenuService.list(query);
		int total = staffMenuService.count(query);
		PageUtils pageUtils = new PageUtils(staffMenuList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "staff/staffMenu/add";
	}

	@GetMapping("/edit/{smenuId}")
	@RequiresPermissions("staff:staffMenu:edit")
	String edit(@PathVariable("smenuId") Long smenuId,Model model){
		StaffMenuDO staffMenu = staffMenuService.get(smenuId);
		model.addAttribute("staffMenu", staffMenu);
	    return "staff/staffMenu/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( StaffMenuDO staffMenu){
		if(staffMenuService.save(staffMenu)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( StaffMenuDO staffMenu){
		staffMenuService.update(staffMenu);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long smenuId){
		if(staffMenuService.remove(smenuId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] smenuIds){
		staffMenuService.batchRemove(smenuIds);
		return R.ok();
	}

	@GetMapping("/tree")
	@ResponseBody
	Tree<StaffMenuDO> tree() {
		Tree<StaffMenuDO> tree = new Tree<StaffMenuDO>();
		tree = staffMenuService.getTree();
		return tree;
	}
	
	@GetMapping("/tree/{staffId}")
	@ResponseBody
	Tree<StaffMenuDO> tree(@PathVariable("staffId") Long staffId) {
		Tree<StaffMenuDO> tree = new Tree<StaffMenuDO>();
		tree = staffMenuService.getTree(staffId);
		return tree;
	}
}
